﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _430GroupProject;

namespace WebClient.AdministrativeTools
{
    public partial class UserManagement : System.Web.UI.Page
    {
        List<User> users;

        protected void Page_Load(object sender, EventArgs e)
        {
            User user = (User)Session["user"];

            if (user == null || !user.Permissions.Contains(Permission.Admin))
            {
                Response.Redirect("~/NotAuthorized.aspx");
            }
        }

        protected void AddUserButton_Click(object sender, EventArgs e)
        {
            ViewUsersPanel.Visible = false;
            CreateNewUser.Visible = true;
        }

        protected void ViewCurrentUsers_Click(object sender, EventArgs e)
        {
            ViewUsersPanel.Visible = true;
            CreateNewUser.Visible = false;
            EditUserPanel.Visible = false;
        }

        protected void CreateUserButton_Click(object sender, EventArgs e)
        {
            if (UserName.Text.Equals("") || FirstName.Text.Equals("") || LastName.Text.Equals("") || UniversityID.Text.Equals("") || Email.Text.Equals(""))
            {
            }
            else
            {
                User toAdd = new User(UserName.Text, UniversityID.Text, Email.Text, LastName.Text + ", " + FirstName.Text);
                toAdd.Permissions = new List<Permission>();
                if (NewUserPayroll.Checked)
                {
                    toAdd.Permissions.Add(Permission.Payroll);
                }

                if (NewUserAdmin.Checked)
                {
                    toAdd.Permissions.Add(Permission.Admin);
                }
                //Encrypt password before send off.
                System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] data = System.Text.Encoding.ASCII.GetBytes("430Pass");
                data = x.ComputeHash(data);
                toAdd.Password = System.Text.Encoding.ASCII.GetString(data);
                
                Master.Proxy.addUser(toAdd);
                this.ViewCurrentUsers.Visible = true;
                this.CreateNewUser.Visible = false;
                this.UpdatePanel.Update();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string userID = QueryNetID.Text;
            object user = Master.Proxy.getUser(userID);

        }

        protected void GetUserButton_Click(object sender, EventArgs e)
        {
            string userID = QueryNetID.Text;
            User user = Master.Proxy.getUser(userID);

            if (user== null || (user.Error != null &&!user.Error.Header.Equals("")) )
            {
                this.ErrorPanel.Visible = true;
                this.ErrorText.Text = "The user you have searched for, " + userID + " is not in the system";
                this.UpdatePanel.Update();

            }
            else
            {
                EditUserPanel.Visible = true;
                ViewUsersPanel.Visible = false;
                UserIDLabel.Text = "Unique User ID: " + user.UserID;
                EditNetID.Text = user.NetID;
                EditUserPassword.Text = user.Password;
                EditUserName.Text = user.FullName;
                EditUserUniversityID.Text = user.UniversityID;
                EditEmail.Text = user.Email;
                PayrollEditUser.Checked = user.Permissions.Contains(Permission.Payroll);
                AdminEditUser.Checked = user.Permissions.Contains(Permission.Admin);
            }
        }

        protected void EditUserButton_Click(object sender, EventArgs e)
        {
            User toEdit = new User(EditNetID.Text, EditUserUniversityID.Text, EditEmail.Text, EditUserName.Text);
            toEdit.NetID = EditNetID.Text;
            toEdit.FullName = EditUserName.Text;
            toEdit.UniversityID = EditUserUniversityID.Text;
            if (toEdit.Permissions == null)
            {
                toEdit.Permissions = new List<Permission>();
            }
            if (PayrollEditUser.Checked)
            {
                toEdit.Permissions.Add(Permission.Payroll);
            }

            if (AdminEditUser.Checked)
            {
                toEdit.Permissions.Add(Permission.Admin);
            }
            Master.Proxy.updateUser(toEdit);

            //encrypt new password
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(EditUserPassword.Text);
            data = x.ComputeHash(data);
            String passMd5Hash = System.Text.Encoding.ASCII.GetString(data);

            if (toEdit.Password == null || !toEdit.Password.Equals(passMd5Hash))
            {
                Master.Proxy.updatePassword(toEdit.NetID, passMd5Hash);
            }

            Session["EditingUser"] = null;
            EditUserPanel.Visible = false;
            ViewUsersPanel.Visible = true;

            if (UserAccounts.DataSource != null) //We should reload the grid so the user will see a change.  TODO: Change if dataset becomes to large
            {
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void GetUsersButton_Click(object sender, EventArgs e)
        {
            Users accounts = Master.Proxy.getAllUsers();
            UserAccounts.DataSource = accounts.Accounts;
            UserAccounts.DataBind();
        }

        protected void UserAccounts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = UserAccounts.Rows[index];
            if (e.CommandName == "Edit")
            {
                EditUserPanel.Visible = true;
                ViewUsersPanel.Visible = false;
                
                
                
                EditNetID.Text = row.Cells[0].Text;
                EditUserUniversityID.Text = row.Cells[1].Text;
                EditEmail.Text = row.Cells[2].Text;
                EditUserName.Text = row.Cells[3].Text;
                EditUserPassword.Text = row.Cells[4].Text;

            }
            
        }

        protected void UserAccounts_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void ErrorClose_Click(object sender, EventArgs e)
        {
            ErrorPanel.Visible = false;
        }

    }
}